using System;
using System.Collections.Generic;
using System.Text;
using Forecast.Database;

namespace Forecast.Business
{
    public class Results
    {
        #region Interface

        public int GetResultForPlayer(Player player, Prediction prediction, Holding holding, IList<Offer> offers)
        {
            int result = -2500;  // Original price paid
            result += holding.NumPredictions * prediction.LastValue;  // Value now

            foreach (Offer offer in offers)
            {
                if (!offer.IsAccepted && !offer.IsActive)
                    continue;
                if (offer.PredictionId != prediction.Id)
                    continue;
                if (player.Id == offer.PlayerId)
                {
                    if (offer.IsBuy && offer.IsAccepted)  
                        result -= (offer.Price * offer.NumPredictions);
                    if (!offer.IsBuy)
                    {
                        int price = offer.Price;
                        if (!offer.IsAccepted)
                        {
                            if (prediction.LastValue < offer.Price)
                                price = prediction.LastValue;
                        }
                        result += (price * offer.NumPredictions);
                    }
                }
                else
                {
                    if (offer.IsBuy)  
                        result += (offer.Price * offer.NumPredictions);
                    if (!offer.IsBuy)  
                        result -= (offer.Price * offer.NumPredictions);
                }
            }

            return result;
        }

        #endregion
    }
}
